"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const FairyEditor = CS.FairyEditor;
const FindDependencyQuery_1 = require("../WindowView/FindDependencyQuery");
const ComBasicPlugin_1 = require("./ComBasic/ComBasicPlugin");
const UIWind_1 = require("../WindowView/UIWind");
const FindFailureAssets_1 = require("../WindowView/FindFailureAssets");
const CustomName_1 = require("../WindowView/CustomName");
const ControllerEditDialog_1 = require("../WindowView/ControllerEditDialog");
const CustomPlugin_1 = require("../WindowView/CustomPlugin");
const NodeThree_1 = require("../WindowView/NodeThree");
const SG_1 = require("../SG");
const ToolMgr_1 = require("../ToolMgr");
const App = FairyEditor.App;
class ToolPlugin {
    Register() {
        this.registerMenu();
        this.registerInspector();
    }
    registerMenu() {
        let toolmenu = App.menu.GetSubMenu("tool");
        toolmenu.AddSeperator();
        toolmenu.AddItem("自定义插件", "openCustomPlugin", this.openCustomPlugin.bind(this));
        toolmenu.AddItem("查找组件依赖关系", "findDependencyQuery", this.findDependencyQuery.bind(this));
        toolmenu.AddItem("查找失效资源", "FindFailureAssets", this.FindFailureAssets.bind(this));
        if (SG_1.default.config[SG_1.Config.OPENCUSTOMNAME])
            toolmenu.AddItem("扩展名称下拉框", "customName", this.customName.bind(this));
        if (SG_1.default.config[SG_1.Config.OPENCONTROLLERNAME]) {
            toolmenu.AddItem("扩展控制器下拉框", "customControllerName", this.customControllerName.bind(this));
            this.updateControllerEditDialogDownDropNames();
        }
        toolmenu.AddItem("打开节点树", "OpenThree", this.OpenThree.bind(this));
        /**右键目录 */
        let contextmenu = App.libView.contextMenu;
        //contextmenu.AddSeperator()
    }
    //#region 工具
    findDependencyQuery() {
        FindDependencyQuery_1.default.show();
    }
    openCustomPlugin() {
        CustomPlugin_1.default.show();
    }
    FindFailureAssets() {
        FindFailureAssets_1.default.show();
    }
    registerInspector() {
        ComBasicPlugin_1.default.create();
    }
    customName() {
        CustomName_1.default.show();
    }
    customControllerName() {
        ControllerEditDialog_1.default.show();
    }
    /**刷新控制器弹窗下拉列表扩展 */
    updateControllerEditDialogDownDropNames() {
        let controllerPanel = App.docView.GetChildAt(0).asCom.GetChild('controllerPanel');
        controllerPanel.asCom.GetChild("list").asList.onClickItem.Add(this.onControllerEditDialogDownDropListItemClick.bind(this));
    }
    onControllerEditDialogDownDropListItemClick(context) {
        return __awaiter(this, void 0, void 0, function* () {
            if (context.data.mode != 0)
                return;
            while (true) {
                let window = null;
                for (let index = 0; index < App.groot.numChildren; index++) {
                    window = App.groot.GetChildAt(index);
                    if (window.GetType().ToString() != "FairyEditor.Dialog.ControllerEditDialog") {
                        window = null;
                        continue;
                    }
                }
                if (window == null || window.contentPane == null) {
                    yield ToolMgr_1.default.sleep(1.5);
                    continue;
                }
                let name = window.contentPane.GetChild("name");
                //自定义名称扩展
                if (SG_1.default.config[SG_1.Config.OPENCONTROLLERNAME]) {
                    let c_name = name.asComboBox;
                    let list = ToolMgr_1.default.createGenericList(CS.System.String);
                    let datas = SG_1.default.config[SG_1.Config.CUSTOMCONTROLLERNAME];
                    let keys = Object.keys(datas);
                    keys.forEach(element => {
                        list.Add(element);
                    }, this);
                    c_name.values = list.ToArray();
                    c_name.items = list.ToArray();
                    c_name.text = context.data.name;
                    c_name.onChanged.Remove(this.onControllerEditDialogNameChanged);
                    c_name.onChanged.Add(this.onControllerEditDialogNameChanged);
                }
                return;
            }
        });
    }
    /**改变控制器名称时刷新控制器弹窗下拉列表 */
    onControllerEditDialogNameChanged() {
        let datas = SG_1.default.config[SG_1.Config.CUSTOMCONTROLLERNAME];
        let window = null;
        for (let index = 0; index < App.groot.numChildren; index++) {
            window = App.groot.GetChildAt(index);
            if (window.GetType().ToString() != "FairyEditor.Dialog.ControllerEditDialog") {
                window = null;
                continue;
            }
        }
        let name = window.contentPane.GetChild("name");
        let pageList = window.contentPane.GetChild("pageList");
        let addPage = window.contentPane.GetChild("addPage");
        let removePage = window.contentPane.GetChild("removePage");
        let c_name = name.asComboBox;
        pageList.selectedIndex = -1;
        let data = datas[c_name.value];
        let alias = window.contentPane.GetChild("alias").asLabel;
        alias.text = data.remark;
        alias.GetChild("title").asTextInput.onFocusOut.Call();
        if (pageList.numChildren > data.values.length) {
            for (let index = pageList.numChildren - 1; index >= data.values.length; index--) {
                pageList.selectedIndex = pageList.numChildren - 1;
                removePage.onClick.Call();
            }
        }
        else {
            for (let index = pageList.numChildren; index < data.values.length; index++) {
                addPage.onClick.Call();
            }
        }
        for (let index = 0; index < data.values.length; index++) {
            //刷新名称
            let label_name = pageList.GetChildAt(index).asButton.GetChild("name").asLabel;
            let input_name = label_name.GetChild("input").asTextInput;
            input_name.inputTextField.text = data.values[index] ? data.values[index] : "";
            label_name.text = data.values[index] ? data.values[index] : "";
            input_name.onFocusOut.Call();
            //刷新备注
            let label_alias = pageList.GetChildAt(index).asButton.GetChild("remark").asLabel;
            let input_alias = label_alias.GetChild("input").asTextInput;
            input_alias.inputTextField.text = data.values_remark[index] ? data.values_remark[index] : "";
            label_alias.text = data.values_remark[index] ? data.values_remark[index] : "";
            input_alias.onFocusOut.Call();
        }
    }
    OpenThree() {
        NodeThree_1.default.show();
    }
    //#endregion
    //#region 右键目录
    //#endregion
    onDestroy() {
        let toolmenu = App.menu.GetSubMenu("tool");
        toolmenu.RemoveItem("openCustomPlugin");
        toolmenu.RemoveItem("findDependencyQuery");
        toolmenu.RemoveItem('FindFailureAssets');
        toolmenu.RemoveItem('customName');
        toolmenu.RemoveItem('customControllerName');
        toolmenu.RemoveItem('OpenThree');
        let contextmenu = App.libView.contextMenu;
        UIWind_1.default.delAll([]);
        App.consoleView.Log("插件卸载");
    }
}
exports.default = ToolPlugin;
